﻿/*   
    Copyright (C) 2009 Galaktika Corporation ZAO

    This file is a part of Galaktika.BusinessMonitor
 
    Galaktika.BusinessMonitor is a free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.
      
    You should have received a copy of the GNU General Public License
    along with Galaktika.BusinessMonitor.  If not, see <http://www.gnu.org/licenses/>.
  
    If GPL v.3 is not suitable for your products or company,
    Galaktika Corp provides Galaktika.BusinessMonitor under a flexible commercial license
    designed to meet your specific usage and distribution requirements.
    If you have already obtained a commercial license from Galaktika Corp,
    you can use this file under those license terms.
*/

using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace Galaktika.BI.Silverlight.ClientApp.Items
{
    using Galaktika.BI.Silverlight.Controls;
    using System.Collections.Generic;
    using System.Windows.Markup;

    public class TabPageContainer : TabControl
    {
        public TabPageContainer()
        {
            base.SetValue(TabPagesProperty, new TabPageCollection(this));
            DefaultStyleKey = typeof(TabPageContainer); 
        }

        public FrameworkElement GetControlFromTemplate(String nameFromTemplate)
        {
            return base.GetTemplateChild(nameFromTemplate) as FrameworkElement;
        }

        private static readonly DependencyProperty TabPagesProperty = DependencyProperty.Register(
            "TabPages", typeof(TabPageCollection), typeof(TabPageContainer), null);
        public TabPageCollection TabPages
        {
            get
            {
                return (TabPageCollection)base.GetValue(TabPagesProperty);
            }
        }

        public TabPage AddPage(string text, string hint)
        {
            return this.AddPage(text, hint, null);
        }

        public TabPage AddPage(string text, string hint, object content)
        {
            TabPage page = this.TabPages.AddInternal(new TabPageItem(text, content));
            page.Hint = hint;
            return page;
        }

        public TabPage AddPage(string text, string hint, object content, bool isClosable)
        {
            TabPage page = this.TabPages.AddInternal(new TabPageItem(text, content));
            page.CanClose = isClosable;
            page.Hint = hint;
            return page;
        }

        internal void page_QueryClose(object sender, CancelEventArgs e)
        {
            this.OnTabPageClosing(e);
            if (!e.Cancel)
            {
                TabPage page = (TabPage)sender;
                TabPageItem item = (TabPageItem)page.Tag;
                this.TabPages.Remove(item);

                this.OnTabPageClosed(new TabPageClosedEventArgs(page));
            }
        }

        #region events

        protected override void OnSelectionChanged(SelectionChangedEventArgs args)
        {
            base.OnSelectionChanged(args);
        }
        public event CancelEventHandler TabPageClosing;
        protected void OnTabPageClosing(CancelEventArgs e)
        {
            if (this.TabPageClosing != null)
            {
                this.TabPageClosing(this, e);
            }
        }

        public event TabPageClosedEventHandler TabPageClosed;
        protected void OnTabPageClosed(TabPageClosedEventArgs e)
        {
            if (this.TabPageClosed != null)
            {
                this.TabPageClosed(this, e);
            }
        }
        #endregion

        
    }

    #region TabCollection
    public class TabPageItem : DependencyObject
    {
        public TabPageItem()
        {
        }

        public TabPageItem(string caption, object content)
        {
            this.Caption = caption;
            this.Content = content;
        }

        private static readonly DependencyProperty CaptionProperty = DependencyProperty.Register(
            "Caption", typeof(string), typeof(TabPageItem), null);
        public string Caption
        {
            get
            {
                return (string)base.GetValue(CaptionProperty);
            }
            set
            {
                base.SetValue(CaptionProperty, value);
            }
        }

        private static readonly DependencyProperty ContentProperty = DependencyProperty.Register(
            "Content", typeof(object), typeof(TabPageItem), null);
        public object Content
        {
            get
            {
                return base.GetValue(ContentProperty);
            }
            set
            {
                base.SetValue(ContentProperty, value);
            }
        }
    }

    public class TabPageCollection : System.Collections.Generic.ICollection<TabPageItem>
    {
        private Dictionary<TabPageItem, TabPage> m_InternalColl;
        private TabPageContainer m_Owner;
        internal TabPageCollection(TabPageContainer owner)
        {
            m_InternalColl = new Dictionary<TabPageItem, TabPage>();
            m_Owner = owner;
        }

        internal TabPage AddInternal(TabPageItem item)
        {
            TabPage page = new TabPage();
            page.Text = item.Caption;
            if (item.Content != null)
            {
                page.Content = item.Content;
            }
            page.QueryClose += new CancelEventHandler(m_Owner.page_QueryClose);

            m_Owner.Items.Add(page);

            m_InternalColl.Add(item, page);
            page.Tag = item;
            return page;
        }

        public TabPage this[int index]
        {
            get
            {
                return m_Owner.Items[index] as TabPage;
            }
        }

        #region ICollection<TabPageItem> Members

        public void Add(TabPageItem item)
        {
            this.AddInternal(item);
        }

        public void Clear()
        {
            m_InternalColl.Clear();
        }

        public bool Contains(TabPageItem item)
        {
            return m_InternalColl.ContainsKey(item);
        }

        public void CopyTo(TabPageItem[] array, int arrayIndex)
        {
            m_InternalColl.Keys.CopyTo(array, arrayIndex);
        }

        public int Count
        {
            get { return m_InternalColl.Count; }
        }

        public bool IsReadOnly
        {
            get { return false; }
        }

        public bool Remove(TabPageItem item)
        {
            TabPage page = m_InternalColl[item];
            page.QueryClose -= new CancelEventHandler(m_Owner.page_QueryClose);
            m_Owner.Items.Remove(page);
            return m_InternalColl.Remove(item);
        }

        #endregion

        #region IEnumerable<TabPageItem> Members

        public System.Collections.Generic.IEnumerator<TabPageItem> GetEnumerator()
        {
            return m_InternalColl.Keys.GetEnumerator();
        }

        #endregion

        #region IEnumerable Members

        System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
        {
            return ((System.Collections.IEnumerable)m_InternalColl.Keys).GetEnumerator();
        }

        #endregion
    }
    #endregion

    public class TabPageClosedEventArgs : EventArgs
    {
        public TabPageClosedEventArgs(TabPage page)
        {
            this.TabPage = page;
        }

        public TabPage TabPage { get; private set; }
    }

    public delegate void TabPageClosedEventHandler(object sender, TabPageClosedEventArgs e);
}